package gao.xiaolei.controller;

import gao.xiaolei.dto.TestRunInDto;
import gao.xiaolei.dto.TestRunOutDto;
import gao.xiaolei.dto.TestTreeDto;
import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.entity.TestRun;
import gao.xiaolei.exception.AuthException;
import gao.xiaolei.exception.LeafException;
import gao.xiaolei.exception.StatusException;
import gao.xiaolei.service.TestRunService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/common/testRun")
public class TestRunController {

    @Resource(name = "TestRunService")
    private TestRunService testRunService;

    @PostMapping("")
    public TestRunOutDto addTestRun(@RequestBody TestRunInDto testRunInDto) {
        return testRunService.addTestRun(testRunInDto);
    }

    @DeleteMapping("/{id}/{userId}")
    public void deleteTestRun(@PathVariable("id") String id,
                              @PathVariable("userId") String userId) {
        testRunService.deleteTestRun(id, userId);
    }

    @PutMapping("")
    public TestRunOutDto updateTestRun(@RequestBody TestRunInDto testRunInDto) {
        return testRunService.updateTestRun(testRunInDto);
    }

    @PutMapping("/title")
    public void updateTitle(@RequestParam("id") String id,
                            @RequestParam("text") String title,
                            @RequestParam("userId") String userId) {
        testRunService.updateTitle(id, title, userId);
    }

    @PutMapping("/parent")
    public void updateParent(@RequestParam("id") String id,
                             @RequestParam("newParentId") String newParentId,
                             @RequestParam("userId") String userId) {
        testRunService.updateParentId(id, newParentId, userId);
    }

    @GetMapping("/project/{projectId}/{userId}")
    public List<TestTreeDto> getRootTestLab(@PathVariable("projectId") String projectId,
                                            @PathVariable("userId") String userId) {
        return testRunService.getRootTestLab(projectId, userId);
    }

    //（只提供审核通过的测试执行）
    @GetMapping("/project/isPass/{projectId}")
    public List<TestTreeDto> getRootTestLabIsPass(@PathVariable("projectId") String projectId) {
        return testRunService.getRootTestLabIsPass(projectId);
    }

    @GetMapping("/project/transform/{projectId}")
    public List<TestTreeDto> getRootTestLabForTransform(@PathVariable("projectId") String projectId) {
        return testRunService.getRootTestLabForTransform(projectId);
    }

    @GetMapping("/children/{id}/{userId}")
    public List<TestTreeDto> getChildren(@PathVariable("id") String id,
                                         @PathVariable("userId") String userId) {
        return testRunService.getChildren(id, userId);
    }

    @GetMapping("/childrenIsPass/{id}")
    public List<TestTreeDto> getChildrenIsPass(@PathVariable("id") String id) {
        return testRunService.getChildrenIsPass(id);
    }

    @GetMapping("/childrenIsTestLab/{id}")
    public List<TestTreeDto> getChildrenIsTestLab(@PathVariable("id") String id) {
        return testRunService.getChildrenIsTestLab(id);
    }

    @GetMapping("/detail/{id}")
    public TestRunOutDto getDetail(@PathVariable("id") String id) {
        return testRunService.getDetail(id);
    }

    @GetMapping("/title/{id}")
    public String getTitle(@PathVariable("id") String id) {
        return testRunService.getTitle(id);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else if (e.getClass() == AuthException.class)
            code = 401;
        else if (e.getClass() == StatusException.class)
            code = 402;
        else if (e.getClass() == LeafException.class)
            code = 403;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
